<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="流程名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="业务名称" prop="messageTitle">
        <el-input
          v-model="queryParams.messageTitle"
          placeholder="请输入业务名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="审批时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="defaultTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-tickets"
          :disabled="single"
          @click="handleFlowRecord"
          >流程详情</el-button
        >
      </el-form-item>
    </el-form>
    <!-- v-hasPermi="['workflow:process:finishedExport']" -->
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-tickets"
          :disabled="single"
          @click="handleFlowRecord"
          >流程详情</el-button
        >
        v-hasPermi="['workflow:category:edit']"
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-back"
          :disabled="single"
          @click="handleRevoke"
          >撤回</el-button
        > 
        v-hasPermi="['workflow:category:remove']"
      </el-col>
      <el-col :span="1.5">
         <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button
        > 
      </el-col>
     v-hasPermi="['workflow:category:export']" 
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->
    <el-table
      v-loading="loading"
      :data="finishedList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="流程名称" prop="procDefName">
        <template #default="scope">
          <span
            class="text-#1890ff pointer"
            @click="handleFlowRecord(scope.row)"
            >{{ scope.row.procDefName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="业务名称"
        prop="messageTitle"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="任务节点" prop="taskName" />
      <el-table-column label="流程发起人">
        <template #default="scope">
          <label
            >{{ scope.row.startUserName }}
            <el-tag type="info">{{ scope.row.startDeptName }}</el-tag></label
          >
        </template>
      </el-table-column>
      <el-table-column label="接收时间" prop="createTime" width="180" />
      <el-table-column label="审批时间" prop="finishTime" width="180" />
      <!-- <el-table-column label="耗时" prop="duration" width="180" /> -->
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        v-hasPermi="['workflow:process:query']" 
        v-hasPermi="['workflow:process:revoke']" 
        <template #default="scope">
          <el-button
            type="primary"
            text
            icon="el-icon-tickets"
            @click="handleFlowRecord(scope.row)"
            >流转记录</el-button
          >
          <el-button
            type="primary"
            text
            icon="el-icon-tickets"
            @click="handleRevoke(scope.row)"
            >撤回
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script name="Finished" setup>
import { listFinishedProcess } from '@/api/workflow/process'
import { revokeProcess } from '@/api/workflow/finished'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const { proxy } = getCurrentInstance()
const data = reactive({
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 已办任务列表数据
  finishedList: [],
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  src: '',
  dateRange: [],
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    messageTitle: undefined,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {}
})
const {
  // 遮罩层
  loading,
  // 选中数组
  ids,
  // 非单个禁用
  single,
  // 非多个禁用
  multiple,
  // 显示搜索条件
  showSearch,
  // 总条数
  total,
  // 已办任务列表数据
  finishedList,
  // 弹出层标题
  title,
  // 是否显示弹出层
  open,
  src,
  dateRange,
  // 查询参数
  queryParams,
  // 表单参数
  form,
  // 表单校验
  rules
} = toRefs(data)
const taskId = ref(null)
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])
/** 查询流程定义列表 */
function getList() {
  loading.value = true
  listFinishedProcess(
    proxy.addDateRange(queryParams.value, dateRange.value)
  ).then(response => {
    finishedList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null
  }
  proxy.resetForm('form')
}
function setIcon(val) {
  if (val) {
    return 'el-icon-check'
  } else {
    return 'el-icon-time'
  }
}
function setColor(val) {
  if (val) {
    return '#2bc418'
  } else {
    return '#b3bdbb'
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryForm')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.procInsId)
  taskId.value = selection.map(item => item.taskId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加流程定义'
}
/** 流程流转记录 */
function handleFlowRecord(row) {
  // router.push({
  //   path: '/workflow/process/detail/' + row.procInsId,
  //   query: {
  //     processed: false
  //   }
  // })
  router.push({
    path: '/process/workDetail',
    query: {
      procInsId: row && row.procInsId ? row.procInsId : ids.value.toString(),
      processed: false
    }
  })
}
/** 撤回任务 */
function handleRevoke(row) {
  const params = {
    procInsId: row && row.procInsId ? row.procInsId : ids.value.toString(),
    taskId: row && row.taskId ? row.taskId : taskId.value.toString()
  }
  revokeProcess(params).then(res => {
    proxy.$modal.msgSuccess('操作成功')
    getList()
  })
}
/** 导出按钮操作 */
function handleExport() {
  proxy.downLoadByUrl(
    'bpmn/workflow/process/finishedExport',
    {
      ...queryParams.value
    },
    `wf_finished_process_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
